package engine.util;

public class Timer 
{
    
    private static final float NANO_PER_SEC = 1E9f;
    private static final float SEC_PER_NANO = 1.0f / 1E9f;
    
    
    private long time;
    
    public Timer()
    {
        this(false);
    }
    
    public Timer(boolean startNow)
    {
        if (startNow)
        {
            this.time = this.getSystemTime();
        }
        else
        {
            this.time = 0L;
        }
    }
    
    public boolean isRunning()
    {
        return (this.time != 0L);
    }
    
    public void start()
    {
        this.time = this.getSystemTime();
    }
    
    public void resetAndContinue()
    {
        if (this.isRunning())
        {
            this.start();
        }
    }
    
    public float getTimePassed()
    {
        if (this.isRunning())
        {
            float deltaNanos = (this.getSystemTime() - this.time);
            float deltaSeconds = (deltaNanos * Timer.SEC_PER_NANO);
            return deltaSeconds;
        }
        else
        {
            return 0;
        }
    }
    
    public void moveBackBy(float timeSeconds)
    {
        long deltaTimeNanos = (long) (Timer.NANO_PER_SEC * timeSeconds + 0.5f);
        this.time += deltaTimeNanos;
    }
    
    public void stop()
    {
        this.time = 0L;
    }
    
    public float stopAndReturnTime()
    {
        float result = this.getTimePassed();
        this.stop();
        return result;
    }
    
    public void destroy()
    {
        this.time = -1L;
    }
    
    private long getSystemTime()
    {
        return System.nanoTime();
    }
    
}
